home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / chrome / content / back_scan.js < prev    next >
Text File  |  2006-02-07  |  8KB  |  301 lines

  1. /*
  2.  //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. try 
  28. {
  29.   var ENABLE_BACKSCAN = 1;
  30.  
  31.   const MediaLibrary = new Components.Constructor("@songbird.org/Songbird/MediaLibrary;1", "sbIMediaLibrary");
  32.   const keys = new Array("title", "length", "album", "artist", "genre");
  33.  
  34.   var aMediaLibrary = new MediaLibrary();
  35.   aMediaLibrary = aMediaLibrary.QueryInterface( Components.interfaces.sbIMediaLibrary );
  36.  
  37.   var bsPaused = false;
  38.   var bsDBQuery = null;
  39.   var bsLastRow = 0;
  40.   var bsTry = 0;
  41.   var bsMaxTries = 5;
  42.   var bsSource = new sbIPlaylistsource();
  43.   var bsSkip = 0;
  44.  
  45.   var bsCurQuery = new sbIDatabaseQuery();
  46.   bsCurQuery.SetAsyncQuery( true );
  47.   bsCurQuery.SetDatabaseGUID( "songbird" );
  48.  
  49.   var bsWaitForExecuting = false;
  50.   
  51.   var bsGUIDArray = new Array();
  52.   var bsMetadataArray = new Array();
  53.   var bsMaxArray = 10; // How many tracks to enqueue before writing to the db.
  54.   var bsSubmitQueries = false;
  55.  
  56.   var bsScanningText = new sbIDataRemote( "backscan.status" );
  57.   var bsPlaylistRef = new sbIDataRemote( "playlist.ref" );
  58.   var bsSongbirdStrings = document.getElementById( "songbird_strings" );
  59.   var bsScanningPaused = new sbIDataRemote( "backscan.paused" );
  60.   
  61.   var bsQueryString = "SELECT uuid, url, length, title FROM library where length=\"0\"";
  62.   
  63.   // Init the text box to the last url played (shrug).
  64.   function BSInitialize()
  65.   {
  66.     try
  67.     {
  68.       if ( ENABLE_BACKSCAN )
  69.       {
  70.         // Bind a function to let us pause and unpause
  71.         bsScanningPaused.BindCallbackFunction( BSDataChange );
  72.       
  73.         // Go start an async query on the library
  74.         bsDBQuery = new sbIDatabaseQuery();
  75.         bsDBQuery.SetAsyncQuery( true );
  76.         bsDBQuery.SetDatabaseGUID( "songbird" );
  77.  
  78.         // Fire us off in a little bit
  79.         bsWaitForExecuting = true;
  80.         setTimeout( BSExecute, 5000 );
  81.         
  82.         // And start scanning in the background.
  83.         setInterval( onBackScan, 500 );
  84.       }
  85.     }
  86.     catch ( err )
  87.     {
  88.       alert( err );
  89.     }
  90.   }
  91.  
  92.   function onBackScan()
  93.   {
  94.     try
  95.     {
  96.       if ( ! bsScanningPaused.GetIntValue() )
  97.       {
  98.         // Wait till we're done with things.
  99.         if ( bsWaitForExecuting )
  100.         {
  101.           return;
  102.         }
  103.         if ( ( bsCurQuery ) && ( bsCurQuery.IsExecuting() ) )
  104.         {
  105.           return;
  106.         }
  107.  
  108.         if ( ! bsDBQuery.IsExecuting() && ( SBDataGetIntValue( "media_scan.open" ) == 0 ) )
  109.         {
  110.           // ...otherwise, we need to start the next track
  111.           var result = bsDBQuery.GetResultObject();
  112.           
  113.           // If we're at the end of the list,
  114.           if ( ( result.GetRowCount() == 0 ) || ( bsLastRow >= result.GetRowCount() ) )
  115.           {
  116.             // ...we need to resubmit the query.
  117.             bsWaitForExecuting = true;
  118.             setTimeout( BSExecute, 2000 );
  119.             bsScanningText.SetValue( "" );
  120.           }
  121.           else
  122.           {
  123.             // ...otherwise, try the next one.
  124.             BSNextTrack();
  125.           }
  126.         }
  127.       }
  128.     }
  129.     catch ( err )  
  130.     {
  131.       alert( "Backscan: " + err );
  132.     }
  133.   }
  134.  
  135.   function BSExecute()
  136.   {
  137.     bsDBQuery.ResetQuery();
  138.     bsDBQuery.AddQuery( bsQueryString );
  139.     var ret = bsDBQuery.Execute();
  140.     bsWaitForExecuting = false;
  141.     bsLastRow = 0;
  142.   }
  143.   
  144.   function BSNextTrack()
  145.   {
  146.     // ...otherwise, try the next one.
  147.     var result = bsDBQuery.GetResultObject();
  148.     var count = 0;
  149.     var quit = false;
  150.     
  151.     while ( ( bsLastRow < result.GetRowCount() ) ) 
  152.     {
  153.       var time = result.GetRowCellByColumn( bsLastRow, "length" );
  154.       
  155.       if ( ( time && time.length && time != "0" ) )
  156.       {
  157.         // Skip it, we already have metadata
  158.         bsScanningText.SetValue( "" );
  159.         if ( count++ > 200 )
  160.         {
  161.           bsSubmitQueries = true;
  162.           quit = true;
  163.         }
  164.       }
  165.       else
  166.       {
  167.         var title = result.GetRowCellByColumn( bsLastRow, "title" );
  168.         var uuid = result.GetRowCellByColumn( bsLastRow, "uuid" );
  169.         var url = result.GetRowCellByColumn( bsLastRow, "url" );
  170.         
  171.         var scanning = "Scanning";
  172.         try
  173.         {
  174.           scanning = bsSongbirdStrings.getString("back_scan.scanning");
  175.         } catch(e) {}
  176.         bsScanningText.SetValue( scanning + "..." );
  177.         var metadata = theVLCCore.getURLMetadata(url, keys);
  178.         
  179.         // GRRRRR.
  180.         for ( var i in metadata )
  181.         {
  182.           if ( metadata[i] == null )
  183.           {
  184.             metadata[i] = "";
  185.           }
  186.         }
  187.  
  188.  
  189.         // If the title is null, use the url
  190.         if ( metadata[0] == "" )
  191.         {
  192.           // No, this should already have the url (or something else) there, so just leave it alone.
  193.           metadata[0] = title; // ConvertUrlToDisplayName( url );
  194.         }
  195.         // If the length is null parse a 0, otherwise strip hours. 
  196.         if ( metadata[1] == "" )
  197.         {
  198.           metadata[1] = "0:00";
  199.         }
  200.         else
  201.         {
  202.           metadata[1] = BSStripHoursFromTimeString( metadata[1] );
  203.         }
  204.         if ( metadata[1] == "0" )
  205.         {
  206.           metadata[1] = "0:00";
  207.         }
  208.         
  209.         bsGUIDArray.push( uuid );
  210.         bsMetadataArray.push( metadata );
  211.         
  212.         if ( bsGUIDArray.length >= bsMaxArray )
  213.         {
  214.           bsSubmitQueries = true;
  215.         }
  216.         quit = true;
  217.       }
  218.       
  219.       bsLastRow++;
  220.       
  221.       if ( quit )
  222.       {
  223.         break;
  224.       }
  225.     }
  226.  
  227.     {
  228.       // If we get to the end of the list and we have things to submit, submit them!
  229.       if ( ( bsLastRow == result.GetRowCount() ) && ( bsGUIDArray.length > 0 ) )
  230.       {
  231.         bsSubmitQueries = true;
  232.       }
  233.  
  234.       if ( bsSubmitQueries )
  235.       {
  236.         bsSubmitQueries = false;
  237.         var anything = false;
  238.         
  239.         bsCurQuery.ResetQuery();
  240.         aMediaLibrary.SetQueryObject(bsCurQuery);
  241.         
  242.         var text = "";
  243.         for ( var i = 0; i < bsGUIDArray.length; i++ )
  244.         {
  245.           text += bsGUIDArray[ i ] + " - " + keys[ i ] + " - " + bsMetadataArray[ i ] + "\n";
  246.         
  247.           // Go submit the metdadata update, and stash the query so we know when the update is done.
  248.           aMediaLibrary.SetValuesByGUID( bsGUIDArray[i], keys.length, keys, bsMetadataArray[i].length, bsMetadataArray[i], true );
  249.           anything = true;
  250.         }
  251. //        alert( text );
  252.         
  253.         bsGUIDArray = [];
  254.         bsMetadataArray = [];
  255.  
  256.         if ( anything )
  257.         {
  258.           bsCurQuery.Execute();
  259.         }
  260.       }
  261.     }
  262.   }
  263.  
  264.   function BSDataChange()
  265.   {
  266.     var scanning = "Scanning";
  267.     var paused = "Paused";
  268.     try
  269.     {
  270.       scanning = bsSongbirdStrings.getString("back_scan.scanning");
  271.       paused = bsSongbirdStrings.getString("back_scan.paused");
  272.     } catch(e) {}
  273.     if ( bsScanningText.GetValue() != "" )
  274.     {
  275.       if ( bsScanningPaused.GetIntValue() )
  276.       {
  277.         bsScanningText.SetValue( scanning + " " + paused );
  278.       }
  279.       else
  280.       {
  281.         bsScanningText.SetValue( scanning + "..." );
  282.       }
  283.     }
  284.   }
  285.  
  286.   function BSStripHoursFromTimeString( str )
  287.   {
  288.     var retval = str;
  289.     if ( ( str.length == 7 ) && ( str[ 0 ] == "0" ) && ( str[ 1 ] == ":" ) )
  290.     {
  291.       retval = str.substring( 2, str.length );
  292.     }
  293.     return retval;
  294.   }
  295. }
  296. catch ( err )
  297. {
  298.   alert( err )
  299. }
  300.  
  301.